#!/bin/bash

OLDHOME=$HOME
notice_user_report()
{
  if [[ "$USER" == "bot-coin" ]]; then
    export HOME=/remote/iosg/home-2/bot-coin
  else
    export HOME=$OLDHOME
  fi
  source $HOME/miniconda3/etc/profile.d/conda.sh
  conda activate coin2_motion_env_migration
  cd $HOME/coin_epm_dongyuan_longshort/coin
  for exchange in "Binance" "Bybit" "Okex";
  do
    config_path=($(find $HOME/coin_epm_dongyuan_longshort/coin_epm_dongyuan/dongyuan_longshort/position_config -type f -name "Futures.$exchange*"| sort -n | tail -1))
    status_path=($(find $HOME/coin_epm_dongyuan_longshort/coin_epm_dongyuan/dongyuan_longshort/position_status -type f -name "Futures.$exchange*"| sort -n | tail -1))
    ./pyrunner python/experimental/wenge/position_status_check.py \
      --slack=$2 \
      --from_mail="no-reply@joomo.io" \
      --to_mail=$3 \
      --subject="Position Exception Report" \
      --context="" \
      --config_file_path=$config_path\
      --status_file_path=$status_path\
      --exchange=$exchange
  done
  conda deactivate
  unset HOME
}

if [ $# -eq 0 ]; then
  echo "lack of datestr!"
else
  # get datestr
  datestr=$1
  # get slack channel
  slack="#coin_epm_operation"
  if [ $# -ge 2 ]; then
    slack=$2
  fi
  # get email list
  to_mail="wenge@prestolabs.io"
  if [ $# -ge 3 ]; then
    to_mail=$3
  fi
  # send users report
  notice_user_report $datestr $slack $to_mail
fi
